package com.example.springboot.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.example.springboot.controller.dto.LoginDTO;
import com.example.springboot.controller.request.BaseRequest;
import com.example.springboot.controller.request.LoginRequest;
import com.example.springboot.controller.request.PasswordRequest;
import com.example.springboot.entity.Admin;
import com.example.springboot.exception.ServiceException;
import com.example.springboot.mapper.AdminMapper;
import com.example.springboot.service.IAdminService;
import com.example.springboot.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author:
 * @Description: TODO
 * @DateTime: 2022/11/26 17:27
 **/
@Service
@Slf4j
public class AdminService implements IAdminService {

    private static final String DEFAULT_PASSWORD = "123";
    private static final String SAULT = "WZLG";
    @Autowired
    AdminMapper adminMapper;
    @Override
    public List<Admin> list() {
        return adminMapper.list();
    }

    @Override
    public PageInfo<Admin> page(BaseRequest baseRequest) {
        PageHelper.startPage(baseRequest.getPageNum(), baseRequest.getPageSize());
        List<Admin> obj = adminMapper.listBycondition(baseRequest);
        return new PageInfo<>(obj);
    }

    @Override
    public void sava(Admin obj) {
        //默认密码123
        if (StrUtil.isBlank(obj.getPassword())) {
            obj.setPassword(DEFAULT_PASSWORD);
        }
        //存储在数据库中的密码进行md5加密
        obj.setPassword(secureutils(obj.getPassword() + SAULT));
        adminMapper.sava(obj);
    }

    @Override
    public void update(Admin obj) {
        obj.setUpdatetime(new Date());
        adminMapper.update(obj);
    }

    @Override
    public Admin getById(Integer id) {
        return adminMapper.getById(id);
    }

    @Override
    public void delete(Integer id) {
        adminMapper.delete(id);
    }

    @Override
    public LoginDTO login(LoginRequest loginRequest) {
        loginRequest.setPassword(secureutils(loginRequest.getPassword()));
        Admin admin = adminMapper.getByUsernameandPassword(loginRequest);
        if (admin == null) {
            throw new ServiceException("用户名或密码错误");
        }
        LoginDTO loginDTO = new LoginDTO();
        BeanUtils.copyProperties(admin, loginDTO);
        //生成token
        String token = TokenUtils.genToken(String.valueOf(admin.getId()), admin.getPassword(), 15);
        loginDTO.setToken(token);
        return loginDTO;
    }

    @Override
    public void changepass(PasswordRequest passwordRequest) {
        passwordRequest.setNewpass(secureutils(passwordRequest.getNewpass()));
        int count = adminMapper.updatepassword(passwordRequest);
        if (count <= 0) {
            throw new ServiceException("更新失败");
        }
    }

    private String secureutils(String password) {
        return SecureUtil.md5(password + SAULT);
    }
}
